package com.pan.test.socket.example05;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server05 {

    public static String PATH = System.getProperty("user.dir")+"\\picture\\server";
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(11452);
        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        int len;
        String receive = "";
        if ((len = inputStream.read(buf)) != -1){
            receive = new String(buf,0,len);
            System.out.println("收到："+receive);
        }
        File file;
        if (receive.equals("听雨")){
            file = new File(PATH+"\\听雨.m4a");
            System.out.println("发送音乐 听雨.m4a");
        }else {
            file = new File(PATH+"\\İzmir Marşı.m4a");
            System.out.println("发送音乐 İzmir Marşı.m4a");
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(fileInputStream.readAllBytes());
        socket.shutdownOutput();
        System.out.println("发送完成");

        fileInputStream.close();
        outputStream.close();

        inputStream.close();
        socket.close();
        serverSocket.close();

    }
}
